home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / tcpdump-3.0.2 / print-udp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-11  |  7.0 KB  |  263 lines

  1. /*
  2.  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static char rcsid[] =
  24.     "@(#) $Header: print-udp.c,v 1.37 94/06/10 17:01:42 mccanne Exp $ (LBL)";
  25. #endif
  26.  
  27. #include <sys/param.h>
  28. #include <sys/time.h>
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31.  
  32. #include <netinet/in.h>
  33. #include <netinet/in_systm.h>
  34. #include <netinet/ip.h>
  35. #include <netinet/ip_var.h>
  36. #include <netinet/udp.h>
  37. #include <netinet/udp_var.h>
  38.  
  39. #undef NOERROR                    /* Solaris sucks */
  40. #include <arpa/nameser.h>
  41. #include <arpa/tftp.h>
  42.  
  43. #ifdef SOLARIS
  44. #include <tiuser.h>
  45. #endif
  46. #include <rpc/types.h>
  47. #include <rpc/auth.h>
  48. #include <rpc/auth_unix.h>
  49. #include <rpc/svc.h>
  50. #include <rpc/xdr.h>
  51. #include <rpc/rpc_msg.h>
  52.  
  53. #include <errno.h>
  54. #include <stdio.h>
  55.  
  56. #include "interface.h"
  57. #include "addrtoname.h"
  58. #include "appletalk.h"
  59.  
  60. #include "nfsv2.h"
  61. #include "bootp.h"
  62.  
  63. extern int packettype;
  64.  
  65. static void
  66. vat_print(const void *hdr, int len, register const struct udphdr *up)
  67. {
  68.     /* vat/vt audio */
  69.     u_int ts = *(u_short *)hdr;
  70.     if ((ts & 0xf060) != 0) {
  71.         /* probably vt */
  72.         (void)printf(" udp/vt %d %d / %d",
  73.                  ntohs(up->uh_ulen) - sizeof(*up),
  74.                  ts & 0x3ff, ts >> 10);
  75.     } else {
  76.         /* probably vat */
  77.         u_int i0 = ((u_int *)hdr)[0];
  78.         u_int i1 = ((u_int *)hdr)[1];
  79.         printf(" udp/vat %d c%d %u%s",
  80.             ntohs(up->uh_ulen) - sizeof(*up) - 8,
  81.             i0 & 0xffff,
  82.             i1, i0 & 0x800000? "*" : "");
  83.         /* audio format */
  84.         if (i0 & 0x1f0000)
  85.             printf(" f%d", (i0 >> 16) & 0x1f);
  86.         if (i0 & 0x3f000000)
  87.             printf(" s%d", (i0 >> 24) & 0x3f);
  88.     }
  89. }
  90.  
  91. static void
  92. rtp_print(const void *hdr, int len, register const struct udphdr *up)
  93. {
  94.     /* rtp v1 video */
  95.     u_int *ip = (u_int *)hdr;
  96.     u_int i0 = ((u_int *)hdr)[0];
  97.     u_int i1 = ((u_int *)hdr)[1];
  98.     u_int hasopt = i0 & 0x800000;
  99.     u_int contype = (i0 >> 16) & 0x3f;
  100.     printf(" udp/rtp %d c%d %s%s %d",
  101.         ntohs(up->uh_ulen) - sizeof(*up) - 8,
  102.         contype,
  103.         hasopt? "+" : "",
  104.         i0 & 0x400000? "*" : "",
  105.         i0 & 0xffff);
  106.     if (contype == 31) {
  107.         ip += 2;
  108.         len >>= 2;
  109.         len -= 2;
  110.         if (hasopt) {
  111.             u_int i2, optlen;
  112.             do {
  113.                 i2 = ip[0];
  114.                 optlen = (i2 >> 16) & 0xff;
  115.                 if (optlen == 0 || optlen > len) {
  116.                     printf(" !opt");
  117.                     return;
  118.                 }
  119.                 ip += optlen;
  120.             } while ((int)i2 >= 0);
  121.         }
  122.         printf(" 0x%04x", ip[0] >> 16);
  123.     }
  124.     if (vflag)
  125.         printf(" %u", i1);
  126. }
  127.  
  128. /* XXX probably should use getservbyname() and cache answers */
  129. #define TFTP_PORT 69        /*XXX*/
  130. #define SUNRPC_PORT 111        /*XXX*/
  131. #define SNMP_PORT 161        /*XXX*/
  132. #define NTP_PORT 123        /*XXX*/
  133. #define SNMPTRAP_PORT 162    /*XXX*/
  134. #define RIP_PORT 520        /*XXX*/
  135.  
  136. void
  137. udp_print(register const u_char *bp, int length, register const u_char *bp2)
  138. {
  139.     register const struct udphdr *up;
  140.     register const struct ip *ip;
  141.     register const u_char *cp;
  142.     u_short sport, dport, ulen;
  143.  
  144.     up = (struct udphdr *)bp;
  145.     ip = (struct ip *)bp2;
  146.     cp = (u_char *)(up + 1);
  147.     if (cp > snapend) {
  148.         printf("[|udp]");
  149.         return;
  150.     }
  151.     if (length < sizeof(struct udphdr)) {
  152.         (void)printf(" truncated-udp %d", length);
  153.         return;
  154.     }
  155.     length -= sizeof(struct udphdr);
  156.  
  157.     sport = ntohs(up->uh_sport);
  158.     dport = ntohs(up->uh_dport);
  159.     ulen = ntohs(up->uh_ulen);
  160.     if (packettype) {
  161.         register struct rpc_msg *rp;
  162.         enum msg_type direction;
  163.  
  164.         switch (packettype) {
  165.         case 1:
  166.             (void)printf("%s.%s > %s.%s:",
  167.                 ipaddr_string(&ip->ip_src),
  168.                 udpport_string(sport),
  169.                 ipaddr_string(&ip->ip_dst),
  170.                 udpport_string(dport));
  171.             vat_print((void *)(up + 1), length, up);
  172.             break;
  173.         case 2:
  174.             (void)printf("%s.%s > %s.%s:",
  175.                 ipaddr_string(&ip->ip_src),
  176.                 udpport_string(sport),
  177.                 ipaddr_string(&ip->ip_dst),
  178.                 udpport_string(dport));
  179.             wb_print((void *)(up + 1), length);
  180.             break;
  181.         case 3:
  182.             rp = (struct rpc_msg *)(up + 1);
  183.             direction = (enum msg_type)ntohl(rp->rm_direction);
  184.             if (direction == CALL)
  185.                 sunrpcrequest_print((u_char *)rp, length,
  186.                     (u_char *)ip);
  187.             else
  188.                 nfsreply_print((u_char *)rp, length,
  189.                     (u_char *)ip);            /*XXX*/
  190.             break;
  191.         case 4:
  192.             (void)printf("%s.%s > %s.%s:",
  193.                 ipaddr_string(&ip->ip_src),
  194.                 udpport_string(sport),
  195.                 ipaddr_string(&ip->ip_dst),
  196.                 udpport_string(dport));
  197.             rtp_print((void *)(up + 1), length, up);
  198.             break;
  199.         }
  200.         return;
  201.     }
  202.  
  203.     if (! qflag) {
  204.         register struct rpc_msg *rp;
  205.         enum msg_type direction;
  206.  
  207.         rp = (struct rpc_msg *)(up + 1);
  208.         direction = (enum msg_type)ntohl(rp->rm_direction);
  209.         if (dport == NFS_PORT && direction == CALL) {
  210.             nfsreq_print((u_char *)rp, length, (u_char *)ip);
  211.             return;
  212.         }
  213.         else if (sport == NFS_PORT && direction == REPLY) {
  214.             nfsreply_print((u_char *)rp, length, (u_char *)ip);
  215.             return;
  216.         }
  217. #ifdef notdef
  218.         else if (dport == SUNRPC_PORT && direction == CALL) {
  219.             sunrpcrequest_print((u_char *)rp, length, (u_char *)ip);
  220.             return;
  221.         }
  222. #endif
  223.         else if (((struct LAP *)cp)->type == lapDDP &&
  224.             (atalk_port(sport) || atalk_port(dport))) {
  225.             if (vflag)
  226.                 fputs("kip ", stdout);
  227.             atalk_print(cp, length);
  228.             return;
  229.         }
  230.     }
  231.     (void)printf("%s.%s > %s.%s:",
  232.         ipaddr_string(&ip->ip_src), udpport_string(sport),
  233.         ipaddr_string(&ip->ip_dst), udpport_string(dport));
  234.  
  235.     if (!qflag) {
  236. #define ISPORT(p) (dport == (p) || sport == (p))
  237.         if (ISPORT(NAMESERVER_PORT))
  238.             ns_print((const u_char *)(up + 1), length);
  239.         else if (ISPORT(TFTP_PORT))
  240.             tftp_print((const u_char *)(up + 1), length);
  241.         else if (ISPORT(IPPORT_BOOTPC) || ISPORT(IPPORT_BOOTPS))
  242.             bootp_print((const u_char *)(up + 1), length,
  243.                 sport, dport);
  244.         else if (dport == RIP_PORT)
  245.             rip_print((const u_char *)(up + 1), length);
  246.         else if (ISPORT(SNMP_PORT) || ISPORT(SNMPTRAP_PORT))
  247.             snmp_print((const u_char *)(up + 1), length);
  248.         else if (ISPORT(NTP_PORT))
  249.             ntp_print((const u_char *)(up + 1), length);
  250.         else if (dport == 3456)
  251.             vat_print((const void *)(up + 1), length, up);
  252.         /*
  253.          * Kludge in test for whiteboard packets.
  254.          */
  255.         else if (dport == 4567)
  256.             wb_print((const void *)(up + 1), length);
  257.         else
  258.             (void)printf(" udp %d", ulen - sizeof(*up));
  259. #undef ISPORT
  260.     } else
  261.         (void)printf(" udp %d", ulen - sizeof(*up));
  262. }
  263.